package neptune.web.client.view.components.dialogs;



import java.util.ArrayList;
import neptune.web.client.WebInterface;
import neptune.web.client.commands.OpenClusterViewCommand;
import neptune.web.client.model.cluster.SerializablePhysicalNode;
import neptune.web.client.model.topology.SerializableCpu;
import neptune.web.client.model.topology.SerializableIPAddress;
import neptune.web.client.model.topology.SerializableMACAddress;
import neptune.web.client.model.topology.SerializableNetworkInterface;
import neptune.web.client.model.topology.SerializableOperativeSystem;
import neptune.web.client.services.ClusterService;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import neptune.web.client.view.components.MessageWidget;
import neptune.web.client.view.input.*;
/**
 * 
 * @author Luigi Lorenzo Arpino [luigiarpino@yahoo.it]
 *
 */
public class AddPhysicalMachineDialog extends DialogBox implements ClickListener 
{

	private TextBox textNamePhysicalMachine=new TextBox();
	private TextBox textDescPhysicalMachine=new TextBox();
	
	private TextBox textIpAddress = new TextBox();
	private PasswordTextBox textRootPasswd=new PasswordTextBox();
	
	
	public AddPhysicalMachineDialog()
	{
		super(false, true);
		FlexTable mainPanel = new FlexTable();
		mainPanel.setWidth("95%");
		ScrollPanel mainPanelWrapper = new ScrollPanel(mainPanel);
		mainPanelWrapper.setHeight("130px");

		/*
		 * Inizio costruzione campi per inserimemnto dati
		 */
		// Name
		Label labelName = new Label("Name");
		labelName.addStyleName("NodePropsDialog-label");
		this.textNamePhysicalMachine = new TextBox();
		this.textNamePhysicalMachine.addStyleName("NodePropsDialog-input");
		mainPanel.setWidget(0, 0, labelName);
		mainPanel.setWidget(0, 1, this.textNamePhysicalMachine);

		// DESCRIPTION
		Label labelDescription = new Label("Description");
		labelDescription.addStyleName("NodePropsDialog-label");
		this.textDescPhysicalMachine = new TextBox();
		this.textDescPhysicalMachine.addStyleName("NodePropsDialog-input");
		mainPanel.setWidget(1, 0, labelDescription);
		mainPanel.setWidget(1, 1, this.textDescPhysicalMachine);
		
		// IP
		Label labelIP = new Label("Ip address");
		labelIP.addStyleName("NodePropsDialog-label");
		this.textIpAddress = new TextBox();
		this.textIpAddress.addStyleName("NodePropsDialog-input");
		mainPanel.setWidget(3, 0, labelIP);
		mainPanel.setWidget(3, 1, this.textIpAddress );

		// root password
		Label labelPasswd = new Label("Root passwd");
		labelPasswd.addStyleName("NodePropsDialog-label");
		this.textRootPasswd = new PasswordTextBox();
		this.textRootPasswd.addStyleName("NodePropsDialog-input");
		mainPanel.setWidget(4, 0, labelPasswd);
		mainPanel.setWidget(4, 1, this.textRootPasswd);

		// BUTTON PANEL
		HorizontalPanel buttonPanel = new HorizontalPanel();
		Button ok = new Button("Ok");
		ok.addClickListener(this);
		ok.addStyleName("NodePropsDialog-button");
		Button cancel = new Button("Cancel");
		cancel.addClickListener(this);
		cancel.addStyleName("NodePropsDialog-button");
		buttonPanel.add(ok);
		buttonPanel.add(cancel);

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.add(mainPanelWrapper);
		vPanel.add(buttonPanel);
		vPanel.setCellHorizontalAlignment(buttonPanel, VerticalPanel.ALIGN_RIGHT);

		this.setWidget(vPanel);
		this.setText("Add physical machine");

		// Styles
		mainPanelWrapper.addStyleName("NodePropsDialog-mainWrapper");
		mainPanel.addStyleName("NodePropsDialog-mainPanel");
		this.addStyleName("NodePropsDialog");

	}
	
	
	public void onClick(Widget sender)
	{
		if( ((Button) sender).getText().equals("Ok") )
		{
			IPInputValidator ipVal=new IPInputValidator();
			if ( ipVal.isValid(this.textIpAddress.getText() ) )
			{
				// TODO leggere i dati dalla form
				SerializablePhysicalNode serializablePhysicalNode=new SerializablePhysicalNode();
				serializablePhysicalNode.setDescription(this.textDescPhysicalMachine.getText());
				serializablePhysicalNode.setName( this.textNamePhysicalMachine.getText() );
				
				SerializableNetworkInterface sNetInterface=new SerializableNetworkInterface();
				sNetInterface.setBindingDescription("physical");
				SerializableIPAddress ipAddress=new SerializableIPAddress();
				ipAddress.setAddress(this.textIpAddress.getText());
				ipAddress.setSubnetMask("225.225.225.0");
				ipAddress.setControlAddress(true);
				ipAddress.setVersion(4);
			
				ArrayList<SerializableIPAddress> ipAddresses=new ArrayList<SerializableIPAddress>();
				ipAddresses.add(ipAddress);
				sNetInterface.setIpAddresses(ipAddresses);
				// Imposto il MAC Address
				SerializableMACAddress sMacAddress=new SerializableMACAddress();
				//sMacAddress.setAddress(this.textMAC.getText());
				sNetInterface.setMacAddress(sMacAddress);
				sNetInterface.setInterfaceName("eth0");
				ArrayList<SerializableNetworkInterface> al=new ArrayList<SerializableNetworkInterface>();
				al.add(sNetInterface);
				serializablePhysicalNode.setInterfaces(al);
				// Imposto il sistema operativo
				SerializableOperativeSystem sOperSystem=new SerializableOperativeSystem();
				sOperSystem.setAdminUsername("root");
				sOperSystem.setAdminPassword(this.textRootPasswd.getText());
				sOperSystem.setName("Linux");
				sOperSystem.setVersion("2.26");
				serializablePhysicalNode.setOperativeSystem(sOperSystem);
				// Imposto la CPU
				SerializableCpu sCPU=new SerializableCpu();
				sCPU.setClock(0);
				sCPU.setName("undef");
				sCPU.setType("undef");
				serializablePhysicalNode.setCpu(sCPU);
				// Imposto Ram e HD 
				//serializablePhysicalNode.setRam( Integer.valueOf(this.textRAM.getText()) );
				//serializablePhysicalNode.setHd( Float.valueOf(this.textHD.getText()) );

				String message = "Adding physical machine ...";
				final PopupPanel popup = WebInterface.showMessageDialog(
						new MessageWidget(MessageWidget.LOADING, message), false);
				
				ClusterService.Util.getInstance().addPhysicalMachine(serializablePhysicalNode, new AsyncCallback<Boolean>() 
				{
		            public void onFailure(Throwable caught) 
		            {
		            	popup.hide();
		            	String message=new String("Error cause: "+caught.getMessage() );
		            	WebInterface.addMessageToConsoleAndShowDialog(message,true);
		            }

		            public void onSuccess(Boolean isAdded) 
		            {
		            	popup.hide();
		            	OpenClusterViewCommand ocw=new OpenClusterViewCommand();
		            	ocw.execute();
		            	String message=new String("Physical machine added!");
		            	WebInterface.addMessageToConsoleAndShowDialog(message,false);
		            }
		        });
				this.hide();
				resetWindow();
			}
			else
			{
				this.textIpAddress.addStyleName("NodePropsDialog-input-error");
			}
			
		}
		else if ( ((Button) sender).getText().equals("Cancel") )
		{
			this.hide();
			resetWindow();
		}
		else
		{
			
		}
		
	}
	
	private void resetWindow()
	{
		this.textIpAddress.removeStyleName("NodePropsDialog-input-error");
		this.textNamePhysicalMachine.setText("");
		this.textDescPhysicalMachine.setText("");
		this.textIpAddress.setText("");
		this.textRootPasswd.setText("");
	}
	


}
